package com.wsm.common.business.impl;

import com.wsm.common.annotation.RedisCache;
import com.wsm.common.utils.PageUtils;
import com.wsm.common.utils.Query;
import com.wsm.common.utils.Result;
import com.wsm.common.business.ModelBusiness;
import com.wsm.common.domain.ModelDO;
import com.wsm.common.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 系统模板
 *
 * @author wangshimin
 * @email wangshiminhome@foxmail.com
 * @date 2020-02-20 10:21:26
 * @description : 系统模板
 */
@Service
public class ModelBusinessImpl implements ModelBusiness {
	@Autowired
	private ModelService modelService;

	@Override
	@RedisCache(value = "Model" , flush = true, expire = 30L, unit = TimeUnit.MINUTES)
	public ModelDO get(Long id) {
		return modelService.getById(id);
	}

	@Override
	@RedisCache(value = "Model" , flush = false, expire = 30L, unit = TimeUnit.MINUTES)
	public Result<PageUtils> list(Map<String, Object> params) {
		//查询列表数据
		Query query = new Query(params);
		List<ModelDO> modelList = modelService.list(query);
		int total = modelService.count(query);
		PageUtils pageUtils = new PageUtils(modelList, total);
		return Result.success(pageUtils);
	}

	@Override
	@RedisCache(value = "Model" , flush = true, expire = 30L, unit = TimeUnit.MINUTES)
	public Result save( ModelDO model) {
		if (modelService.save(model)) {
			return Result.success();
		}
		return Result.error();
	}

	@Override
	@RedisCache(value = "Model" , flush = true, expire = 30L, unit = TimeUnit.MINUTES)
	public Result update( ModelDO model) {
		if (modelService.updateById(model)) {
			return Result.success();
		}
		return Result.error();
	}

	@Override
	@RedisCache(value = "Model" , flush = true, expire = 30L, unit = TimeUnit.MINUTES)
	public Result remove( Long id) {
		if (modelService.removeById(id)) {
			return Result.success();
		}
		return Result.error();
	}

	@Override
	@RedisCache(value = "Model" , flush = true, expire = 30L, unit = TimeUnit.MINUTES)
	public Result batchRemove(List<Long> ids) {
			modelService.removeByIds(ids);
		return Result.success();
	}

			@Override
		@RedisCache(value = "Model" , flush = true, expire = 30L, unit = TimeUnit.MINUTES)
		public Result removeOrDel(Long id) {
			if (modelService.removeOrDel(id) > 0) {
				return Result.success();
			}
			return Result.error();
		}

		@Override
		@RedisCache(value = "Model" , flush = true, expire = 30L, unit = TimeUnit.MINUTES)
		public Result batchRemoveOrDel(Long[] ids) {
				modelService.batchRemoveOrDel(ids);
			return Result.success();
		}

		@Override
		@RedisCache(value = "Model" , flush = true, expire = 30L, unit = TimeUnit.MINUTES)
		public Result addOrDel(Long id) {
			if (modelService.addOrDel(id) > 0) {
				return Result.success();
			}
			return Result.error();
		}

		@Override
		@RedisCache(value = "Model" , flush = true, expire = 30L, unit = TimeUnit.MINUTES)
		public Result batchAddOrDel(Long[] ids) {
				modelService.batchAddOrDel(ids);
			return Result.success();
		}
	
			@Override
		@RedisCache(value = "Model" , flush = true, expire = 30L, unit = TimeUnit.MINUTES)
		public Result removeUseFlag(Long id) {
			if (modelService.removeUseFlag(id) > 0) {
				return Result.success();
			}
			return Result.error();
		}

		@Override
		@RedisCache(value = "Model" , flush = true, expire = 30L, unit = TimeUnit.MINUTES)
		public Result addUseFlag(Long id) {
			if (modelService.addUseFlag(id) > 0) {
				return Result.success();
			}
			return Result.error();
		}
		
}
